<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="SPU名称">
        {{spu.spuName}}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" v-model="skuInfo.price" type="number"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" placeholder="" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item v-for="(attr,index) in attrInfoList" :key="attr.id" :label="attr.attrName">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" v-for="(attrValue,index) in attr.attrValueList" :key="attrValue.id"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item v-for="(saleAttr,index) in spusaleAttrList" :key="saleAttr.id" :label="saleAttr.saleAttrName">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option :label="saleAttrValue.saleAttrValueName" :value="`${saleAttr.id}:${saleAttrValue.id}`" v-for="(saleAttrValue,index) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table border style="width:100%" :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column prop="prop" width="80" type="selection"></el-table-column>>
          <el-table-column label="图片" prop="prop" width="width">
            <template slot-scope="{row,$index}">
              <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName" width="width"></el-table-column>
          <el-table-column label="操作" prop="prop" width="width">
            <template slot-scope="{row,$index}">
              <el-button type="primary" v-if="row.isDefault==0" @click="changeDefault(row,spuImageList)">设置默认</el-button>
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data () {
    return {
      // 图片数据
      spuImageList: [],
      // 销售属性
      spusaleAttrList: [],
      // 平台属性
      attrInfoList: [],
      skuInfo: {
        // 第一类收集的数据 父组件给的
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // 第二大类，需要通过v-model获取
        skuName: "",
        skuDesc: "",
        skuDefaultImg: "",
        price: '',
        weight: "",
        // 第三类，自己书写代码收集 
        skuImageList: [
          // {
          //   // imgName: "",
          //   // imgUrl: "",
          //   // isDefault: "",
          //   // skuId: 0,
          //   // spuImgId: 0
          // }
        ],
        // 平台属性
        skuAttrValueList: [
          // {
          //   // attrId: 0,
          //   // valueId: 0,
          // }
        ],
        // 收集销售属性列表
        skuSaleAttrValueList: [
          // {
          //   // saleAttrId: 0,
          //   // saleAttrName: "",
          //   // saleAttrValueId: 0,
          //   // saleAttrValueName: "",
          //   // skuId: 0,
          //   // spuId: 0
          // }
        ],
      },
      spu: {},
      // 收集图片的数据字段  收集的字段缺少isDefault字段
      imageList: []
    }
  },
  methods: {
    // 获取skuForm数据
    async getData (category1Id, category2Id, spu) {
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.id
      this.skuInfo.tmId = spu.tmId
      this.spu = spu
      // 获取图片的数据
      let result = await this.$API.spu.reqSpuImageList(spu.id)
      // console.log(SkuImageList);
      if (result.code == 200) {
        let list = result.data
        list.forEach(item => {
          item.isDefault = 0
        });
        this.spuImageList = list
      }
      // 获取销售属性的数据
      let result1 = await this.$API.spu.reqSpuSaleAttrList(spu.id)
      if (result1.code == 200) {
        this.spusaleAttrList = result1.data
      }
      // 获取平台属性的接口
      let result2 = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      if (result2.code == 200) {
        this.attrInfoList = result2.data
      }
    },
    // table表格复选框按钮的事件
    handleSelectionChange (selection) {
      // 获取到用户选中的图片的信息数据，但是需要注意 ，还缺少isDefault字段
      this.imageList = selection
    },
    // 点击设置默认  设置默认和默认的切换操作，
    changeDefault (row, spuImageList) {
      // 除你点击的isDefault都变为0  你点击的变为1
      spuImageList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      // 收集默认图片的地址
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    // 点击取消按钮切换场景
    cancel () {
      this.$emit('changeScene', 0)
      Object.assign(this._data, this.$options.data())
    },
    //保存按钮的回调
    async save () {
      // 整理数据
      const { attrInfoList, spusaleAttrList, skuInfo, imageList } = this
      // let arr = []
      // 将收集到数据整理
      // attrInfoList.forEach(item => {
      //   if (item.attrIdAndValueId) {
      //     const [attrId, valueId] = item.attrIdAndValueId.split(':')
      //     // 携带给服务器参数应该是对象
      //     let obj = { attrId, valueId }
      //     arr.push(obj)
      //   }
      // })
      // prev为累加的结果，每执行一项，就往里面加一对{attrId,valueId}
      // 整理平台属性的数据
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          prev.push({ attrId, valueId })
        }
        return prev
      }, [])
      skuInfo.skuSaleAttrValueList = spusaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev
      }, [])
      // 获取图片的数据
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })
      let result = await this.$API.spu.reqAddSku(skuInfo)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '保存成功' })
        this.$emit('changeScene', 0)
      }
      Object.assign(this._data, this.$options.data())
    }
  }
}
</script>

<style>
</style>